package org.jeecg.modules.charge.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.charge.entity.ChargeInvoiceMangroup;
import org.jeecg.modules.charge.entity.ChargeInvoiceMangroupPosition;
import org.jeecg.modules.charge.entity.ChargeInvoiceMangroupType;
import org.jeecg.modules.charge.entity.ChargeInvoiceManpeople;
import org.jeecg.modules.charge.service.IChargeInvoiceMangroupPositionService;
import org.jeecg.modules.charge.service.IChargeInvoiceMangroupService;
import org.jeecg.modules.charge.service.IChargeInvoiceMangroupTypeService;
import org.jeecg.modules.charge.service.IChargeInvoiceManpeopleService;
import org.jeecg.modules.charge.vo.ChargeInvoiceMangroupPage;
import org.jeecg.modules.charge.vo.CheckOutMangroup;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: 票据组
 * @Author: jeecg-boot
 * @Date:   2019-07-08
 * @Version: V1.0
 */
@RestController
@RequestMapping("/b/charge/chargeInvoiceMangroup")
@Slf4j
public class ChargeInvoiceMangroupController {
	@Autowired
	private IChargeInvoiceMangroupService chargeInvoiceMangroupService;
	@Autowired
	private IChargeInvoiceMangroupTypeService chargeInvoiceMangroupTypeService;
	@Autowired
	private IChargeInvoiceMangroupPositionService chargeInvoiceMangroupPositionService;
	@Autowired
	private IChargeInvoiceManpeopleService chargeInvoiceManpeopleService;
	@Autowired
    private ISysDepartService sysDepartService;
	
	/**
	  * 分页列表查询
	 * @param chargeInvoiceMangroup
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<IPage<ChargeInvoiceMangroup>> queryPageList(ChargeInvoiceMangroup chargeInvoiceMangroup,
									  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									  HttpServletRequest req) {
		Result<IPage<ChargeInvoiceMangroup>> result = new Result<IPage<ChargeInvoiceMangroup>>();
        if (chargeInvoiceMangroup.getGroupNum() != null){
            chargeInvoiceMangroup.setGroupNum("*"+chargeInvoiceMangroup.getGroupNum()+"*");
        }
        if (chargeInvoiceMangroup.getGroupName() !=null){
            chargeInvoiceMangroup.setGroupName("*"+chargeInvoiceMangroup.getGroupName()+"*");
        }
		QueryWrapper<ChargeInvoiceMangroup> queryWrapper = QueryGenerator.initQueryWrapper(chargeInvoiceMangroup, req.getParameterMap());
		Page<ChargeInvoiceMangroup> page = new Page<ChargeInvoiceMangroup>(pageNo, pageSize);
		IPage<ChargeInvoiceMangroup> pageList = chargeInvoiceMangroupService.page(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}
	
	/**
	  *   添加
	 * @param chargeInvoiceMangroupPage
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<ChargeInvoiceMangroup> add(@RequestBody ChargeInvoiceMangroupPage chargeInvoiceMangroupPage) {
		Result<ChargeInvoiceMangroup> result = new Result<ChargeInvoiceMangroup>();
		try {
			ChargeInvoiceMangroup chargeInvoiceMangroup = new ChargeInvoiceMangroup();
			BeanUtils.copyProperties(chargeInvoiceMangroupPage, chargeInvoiceMangroup);
			
			chargeInvoiceMangroupService.save(chargeInvoiceMangroup);
			result.success("添加成功！");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result.error500("操作失败");
		}
		return result;
	}
	
	/**
	  *  编辑
	 * @param chargeInvoiceMangroupPage
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<ChargeInvoiceMangroup> edit(@RequestBody ChargeInvoiceMangroupPage chargeInvoiceMangroupPage) {
		Result<ChargeInvoiceMangroup> result = new Result<ChargeInvoiceMangroup>();
		ChargeInvoiceMangroup chargeInvoiceMangroup = new ChargeInvoiceMangroup();
		BeanUtils.copyProperties(chargeInvoiceMangroupPage, chargeInvoiceMangroup);
		ChargeInvoiceMangroup chargeInvoiceMangroupEntity = chargeInvoiceMangroupService.getById(chargeInvoiceMangroup.getId());
		if(chargeInvoiceMangroupEntity==null) {
			result.error500("未找到对应实体");
		}else {
			chargeInvoiceMangroupService.updateById(chargeInvoiceMangroup);
			result.success("修改成功!");
		}
		
		return result;
	}
	
	/**
	  *   通过id删除
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<ChargeInvoiceMangroup> delete(@RequestParam(name="id",required=true) String id) {
		Result<ChargeInvoiceMangroup> result = new Result<ChargeInvoiceMangroup>();
		ChargeInvoiceMangroup chargeInvoiceMangroup = chargeInvoiceMangroupService.getById(id);
		if(chargeInvoiceMangroup==null) {
			result.error500("未找到对应实体");
		}else {
			chargeInvoiceMangroupService.delMain(id);
			result.success("删除成功!");
		}
		
		return result;
	}
	
	/**
	  *  批量删除
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<ChargeInvoiceMangroup> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		Result<ChargeInvoiceMangroup> result = new Result<ChargeInvoiceMangroup>();
		if(ids==null || "".equals(ids.trim())) {
			result.error500("参数不识别！");
		}else {
			this.chargeInvoiceMangroupService.removeByIds(Arrays.asList(ids.split(",")));
			result.success("删除成功!");
		}
		return result;
	}
	
	/**
	  * 通过id查询
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<ChargeInvoiceMangroup> queryById(@RequestParam(name="id",required=true) String id) {
		Result<ChargeInvoiceMangroup> result = new Result<ChargeInvoiceMangroup>();
		ChargeInvoiceMangroup chargeInvoiceMangroup = chargeInvoiceMangroupService.getById(id);
		if(chargeInvoiceMangroup==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(chargeInvoiceMangroup);
			result.setSuccess(true);
		}
		return result;
	}
	
	//===========================以下是子表信息操作相关API====================================
	
	/**
	  * 通过主表id查询票据组对应票据类型
	 * @param mainId
	 * @return
	 */
	@GetMapping(value = "/listChargeInvoiceMangroupTypeByMainId")
	public Result<List<ChargeInvoiceMangroupType>> queryChargeInvoiceMangroupTypeListByMainId(@RequestParam(name="mainId",required=false) String mainId) {
		Result<List<ChargeInvoiceMangroupType>> result = new Result<List<ChargeInvoiceMangroupType>>();
		List<ChargeInvoiceMangroupType> chargeInvoiceMangroupTypeList = null;
		if (mainId != null) {
			chargeInvoiceMangroupTypeList = chargeInvoiceMangroupTypeService.selectByMainId(mainId);
            result.setResult(chargeInvoiceMangroupTypeList);
            result.setSuccess(true);
            return result;
		}else return null;	
	}
	
	/**
     * 添加票据组对应票据类型
     *
     * @param chargeInvoiceMangroupType
     * @return
     */
    @PostMapping(value = "/addChargeInvoiceMangroupType")
    public Result<ChargeInvoiceMangroupType> addChargeInvoiceMangroupType(@RequestBody ChargeInvoiceMangroupType chargeInvoiceMangroupType) {
        Result<ChargeInvoiceMangroupType> result = new Result<>();
        try {
            ChargeInvoiceMangroup chargeInvoiceMangroup = chargeInvoiceMangroupService.getById(chargeInvoiceMangroupType.getGroupId());
            chargeInvoiceMangroupType.setGroupName(chargeInvoiceMangroup.getGroupName());
            chargeInvoiceMangroupType.setGroupNum(chargeInvoiceMangroup.getGroupNum());
            boolean ok = chargeInvoiceMangroupTypeService.save(chargeInvoiceMangroupType);
            if (ok) {
                result.setSuccess(true);
                result.setMessage("添加票据组对应票据类型成功.");
            } else {
                result.setSuccess(false);
                result.setMessage("添加票据组对应票据类型失败!");
            }
            return result;
        } catch (Exception e) {
            e.fillInStackTrace();
            result.setSuccess(false);
            result.setMessage("添加票据组对应票据类型过程中出现了异常: " + e.getMessage());
            return result;
        }
    }
    
    /**
     * 编辑票据组对应票据类型
     *
     * @param chargeInvoiceMangroupType
     * @return
     */
    @PutMapping("/editChargeInvoiceMangroupType")
    public Result<ChargeInvoiceMangroupType> editChargeInvoiceMangroupType(@RequestBody ChargeInvoiceMangroupType chargeInvoiceMangroupType) {
        Result<ChargeInvoiceMangroupType> result = new Result<>();
        try {
            boolean ok = chargeInvoiceMangroupTypeService.updateById(chargeInvoiceMangroupType);
            if (ok) {
                result.setSuccess(true);
                result.setMessage("更新票据组对应票据类型成功.");
            } else {
                result.setSuccess(false);
                result.setMessage("更新票据组对应票据类型失败!");
            }
            return result;
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("更新数据过程中出现异常啦: " + e.getMessage());
            return result;
        }
    }
    
    /**
     * 通过id删除票据组对应票据类型
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/deleteChargeInvoiceMangroupType")
    public Result<ChargeInvoiceMangroupType> deleteChargeInvoiceMangroupType(@RequestParam(name = "id", required = true) String id) {
        Result<ChargeInvoiceMangroupType> result = new Result<>();
        try {
            boolean ok = chargeInvoiceMangroupTypeService.removeById(id);
            if (ok) {
                result.setSuccess(true);
                result.setMessage("删除票据组对应票据类型成功.");
            } else {
                result.setSuccess(false);
                result.setMessage("删除票据组对应票据类型失败!");
            }
            return result;
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("删除票据组对应票据类型过程中出现异常啦: " + e.getMessage());
            return result;
        }
    }
	
	/**
     * 批量删除票据组对应票据类型
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatchChargeInvoiceMangroupType")
    public Result<ChargeInvoiceMangroupType> deleteBatchChargeInvoiceMangroupType(@RequestParam(name = "ids", required = true) String ids) {
        Result<ChargeInvoiceMangroupType> result = new Result<ChargeInvoiceMangroupType>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            this.chargeInvoiceMangroupTypeService.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }
    
	/**
	  * 通过主表id查询票据组对应岗位
	 * @param mainId
	 * @return
	 */
	@GetMapping(value = "/listChargeInvoiceMangroupPositionByMainId")
	public Result<List<ChargeInvoiceMangroupPosition>> queryChargeInvoiceMangroupPositionListByMainId(@RequestParam(name="mainId",required=false) String mainId) {
		Result<List<ChargeInvoiceMangroupPosition>> result = new Result<List<ChargeInvoiceMangroupPosition>>();
		List<ChargeInvoiceMangroupPosition> chargeInvoiceMangroupPositionList = null;
		if (mainId != null) {
			chargeInvoiceMangroupPositionList = chargeInvoiceMangroupPositionService.selectByMainId(mainId);
            result.setResult(chargeInvoiceMangroupPositionList);
            result.setSuccess(true);
            return result;
		}else return null;	
	}
	
	/**
     * 添加票据组对应岗位
     *
     * @param chargeInvoiceMangroupPosition
     * @return
     */
    @PostMapping(value = "/addChargeInvoiceMangroupPosition")
    public Result<ChargeInvoiceMangroupPosition> addChargeInvoiceMangroupPosition(@RequestBody ChargeInvoiceMangroupPosition chargeInvoiceMangroupPosition) {
        Result<ChargeInvoiceMangroupPosition> result = new Result<>();
        try {
            String officeId = chargeInvoiceMangroupPosition.getOfficeId();
            String id[] = officeId.split(",");
            List<ChargeInvoiceMangroupPosition> MangroupArrayList = new ArrayList<ChargeInvoiceMangroupPosition>();
            ChargeInvoiceMangroup chargeInvoiceMangroup = chargeInvoiceMangroupService.getById(chargeInvoiceMangroupPosition.getGroupId());
            String num=chargeInvoiceMangroup.getGroupNum();
            String name= chargeInvoiceMangroup.getGroupName();
            for(int i=0; i<id.length; i++){
                ChargeInvoiceMangroupPosition position=new ChargeInvoiceMangroupPosition();
                String officeIdSlit = id[i];
                //编号.
                SysDepart sysDepart=sysDepartService.getById(officeIdSlit);
                String offiecName="";
                if(null!=sysDepart){
                //上级部门
                SysDepart upSysDepart=sysDepartService.getById(sysDepart.getParentId());
                    offiecName=upSysDepart.getDepartName();
                }
                String departName=offiecName+sysDepart.getDepartName();
                position.setGroupId(chargeInvoiceMangroupPosition.getGroupId());
                position.setGroupNum(num);
                position.setGroupName(name);
                position.setOfficeId(officeIdSlit);
                position.setOfficeName(departName);
                position.setRemarks(chargeInvoiceMangroupPosition.getRemarks());
                MangroupArrayList.add(position);
            }
            boolean ok = chargeInvoiceMangroupPositionService.saveBatch(MangroupArrayList);
            /*ChargeInvoiceMangroup chargeInvoiceMangroup = chargeInvoiceMangroupService.getById(chargeInvoiceMangroupPosition.getGroupId());
            chargeInvoiceMangroupPosition.setGroupNum(chargeInvoiceMangroup.getGroupNum());
            chargeInvoiceMangroupPosition.setGroupName(chargeInvoiceMangroup.getGroupName());
            System.out.println(chargeInvoiceMangroupPosition);
            boolean ok = chargeInvoiceMangroupPositionService.save(chargeInvoiceMangroupPosition);*/
            if (ok) {
                result.setSuccess(true);
                result.setMessage("添加票据组对应岗位成功.");
            } else {
                result.setSuccess(false);
                result.setMessage("添加票据组对应岗位失败!");
            }
            return result;
        } catch (Exception e) {
            e.fillInStackTrace();
            result.setSuccess(false);
            result.setMessage("添加票据组对应岗位过程中出现了异常: " + e.getMessage());
            return result;
        }
    }
    
    /**
     * 编辑票据组对应岗位
     *
     * @param chargeInvoiceMangroupPosition
     * @return
     */
    @PutMapping("/editChargeInvoiceMangroupPosition")
    public Result<ChargeInvoiceMangroupPosition> editChargeInvoiceMangroupPosition(@RequestBody ChargeInvoiceMangroupPosition chargeInvoiceMangroupPosition) {
        Result<ChargeInvoiceMangroupPosition> result = new Result<>();
        try {
            boolean ok = chargeInvoiceMangroupPositionService.updateById(chargeInvoiceMangroupPosition);
            if (ok) {
                result.setSuccess(true);
                result.setMessage("更新票据组对应岗位成功.");
            } else {
                result.setSuccess(false);
                result.setMessage("更新票据组对应岗位失败!");
            }
            return result;
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("更新数据过程中出现异常啦: " + e.getMessage());
            return result;
        }
    }
    
    /**
     * 通过id删除票据组对应岗位
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/deleteChargeInvoiceMangroupPosition")
    public Result<ChargeInvoiceMangroupPosition> deleteChargeInvoiceMangroupPosition(@RequestParam(name = "id", required = true) String id) {
        Result<ChargeInvoiceMangroupPosition> result = new Result<>();
        try {
            boolean ok = chargeInvoiceMangroupPositionService.removeById(id);
            if (ok) {
                result.setSuccess(true);
                result.setMessage("删除票据组对应岗位成功.");
            } else {
                result.setSuccess(false);
                result.setMessage("删除票据组对应岗位失败!");
            }
            return result;
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("删除票据组对应岗位过程中出现异常啦: " + e.getMessage());
            return result;
        }
    }
	
	/**
     * 批量删除票据组对应岗位
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatchChargeInvoiceMangroupPosition")
    public Result<ChargeInvoiceMangroupPosition> deleteBatchChargeInvoiceMangroupPosition(@RequestParam(name = "ids", required = true) String ids) {
        Result<ChargeInvoiceMangroupPosition> result = new Result<ChargeInvoiceMangroupPosition>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            this.chargeInvoiceMangroupPositionService.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }
    
	/**
	  * 通过主表id查询票据组对应人员
	 * @param mainId
	 * @return
	 */
	@GetMapping(value = "/listChargeInvoiceManpeopleByMainId")
	public Result<List<ChargeInvoiceManpeople>> queryChargeInvoiceManpeopleListByMainId(@RequestParam(name="mainId",required=false) String mainId) {
		Result<List<ChargeInvoiceManpeople>> result = new Result<List<ChargeInvoiceManpeople>>();
		List<ChargeInvoiceManpeople> chargeInvoiceManpeopleList = null;
		if (mainId != null) {
			chargeInvoiceManpeopleList = chargeInvoiceManpeopleService.selectByMainId(mainId);
            result.setResult(chargeInvoiceManpeopleList);
            result.setSuccess(true);
            return result;
		}else return null;	
	}
	
	/**
     * 添加票据组对应人员
     *
     * @param chargeInvoiceManpeople
     * @return
     */
    @PostMapping(value = "/addChargeInvoiceManpeople")
    public Result<ChargeInvoiceManpeople> addChargeInvoiceManpeople(@RequestBody ChargeInvoiceManpeople chargeInvoiceManpeople) {
        Result<ChargeInvoiceManpeople> result = new Result<>();
        ChargeInvoiceMangroup chargeInvoiceMangroup = chargeInvoiceMangroupService.getById(chargeInvoiceManpeople.getGroupId());
        try {
            String[] split = chargeInvoiceManpeople.getUserId().split(",");
            String[] strings = chargeInvoiceManpeople.getUserName().split(",");
            ArrayList<ChargeInvoiceManpeople> chargePeopele = new ArrayList<>();
            for (int i=0;i<split.length;i++){
                ChargeInvoiceManpeople chargeInvoiceManpeople1 = new ChargeInvoiceManpeople();
                chargeInvoiceManpeople1.setUserId(split[i]);
                chargeInvoiceManpeople1.setUserName(strings[i]);
                chargeInvoiceManpeople1.setRemarks(chargeInvoiceManpeople.getRemarks());
                chargeInvoiceManpeople1.setGroupId(chargeInvoiceManpeople.getGroupId());
                chargeInvoiceManpeople1.setPutIn(chargeInvoiceManpeople.getPutIn());
                chargeInvoiceManpeople1.setTakeOut(chargeInvoiceManpeople.getTakeOut());
                chargeInvoiceManpeople1.setCancelAfter(chargeInvoiceManpeople.getCancelAfter());
                chargeInvoiceManpeople1.setCertification(chargeInvoiceManpeople.getCertification());

                chargeInvoiceManpeople1.setGroupName(chargeInvoiceMangroup.getGroupName());
                chargeInvoiceManpeople1.setGroupNum(chargeInvoiceMangroup.getGroupNum());
                chargePeopele.add(chargeInvoiceManpeople1);
            }
            boolean ok = chargeInvoiceManpeopleService.saveBatch(chargePeopele);
            if (ok) {
                result.setSuccess(true);
                result.setMessage("添加票据组对应人员成功.");
            } else {
                result.setSuccess(false);
                result.setMessage("添加票据组对应人员失败!");
            }
            return result;
        } catch (Exception e) {
            e.fillInStackTrace();
            result.setSuccess(false);
            result.setMessage("添加票据组对应人员过程中出现了异常: " + e.getMessage());
            return result;
        }
    }
    
    /**
     * 编辑票据组对应人员
     *
     * @param chargeInvoiceManpeople
     * @return
     */
    @PutMapping("/editChargeInvoiceManpeople")
    public Result<ChargeInvoiceManpeople> editChargeInvoiceManpeople(@RequestBody ChargeInvoiceManpeople chargeInvoiceManpeople) {
        Result<ChargeInvoiceManpeople> result = new Result<>();
        try {
            boolean ok = chargeInvoiceManpeopleService.updateById(chargeInvoiceManpeople);
            if (ok) {
                result.setSuccess(true);
                result.setMessage("更新票据组对应人员成功.");
            } else {
                result.setSuccess(false);
                result.setMessage("更新票据组对应人员失败!");
            }
            return result;
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("更新数据过程中出现异常啦: " + e.getMessage());
            return result;
        }
    }
    
    /**
     * 通过id删除票据组对应人员
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/deleteChargeInvoiceManpeople")
    public Result<ChargeInvoiceManpeople> deleteChargeInvoiceManpeople(@RequestParam(name = "id", required = true) String id) {
        Result<ChargeInvoiceManpeople> result = new Result<>();
        try {
            boolean ok = chargeInvoiceManpeopleService.removeById(id);
            if (ok) {
                result.setSuccess(true);
                result.setMessage("删除票据组对应人员成功.");
            } else {
                result.setSuccess(false);
                result.setMessage("删除票据组对应人员失败!");
            }
            return result;
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("删除票据组对应人员过程中出现异常啦: " + e.getMessage());
            return result;
        }
    }
	
	/**
     * 批量删除票据组对应人员
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatchChargeInvoiceManpeople")
    public Result<ChargeInvoiceManpeople> deleteBatchChargeInvoiceManpeople(@RequestParam(name = "ids", required = true) String ids) {
        Result<ChargeInvoiceManpeople> result = new Result<ChargeInvoiceManpeople>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            this.chargeInvoiceManpeopleService.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }
    

    /**
     * 导出excel
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HttpServletResponse response) {
        // Step.1 组装查询条件
        QueryWrapper<ChargeInvoiceMangroup> queryWrapper = null;
        try {
            String paramsStr = request.getParameter("paramsStr");
            if (oConvertUtils.isNotEmpty(paramsStr)) {
                String deString = URLDecoder.decode(paramsStr, "UTF-8");
                ChargeInvoiceMangroup chargeInvoiceMangroup = JSON.parseObject(deString, ChargeInvoiceMangroup.class);
                queryWrapper = QueryGenerator.initQueryWrapper(chargeInvoiceMangroup, request.getParameterMap());
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //Step.2 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        List<ChargeInvoiceMangroupPage> pageList = new ArrayList<ChargeInvoiceMangroupPage>();
        List<ChargeInvoiceMangroup> chargeInvoiceMangroupList = chargeInvoiceMangroupService.list(queryWrapper);
        for (ChargeInvoiceMangroup chargeInvoiceMangroup : chargeInvoiceMangroupList) {
            ChargeInvoiceMangroupPage vo = new ChargeInvoiceMangroupPage();
            BeanUtils.copyProperties(chargeInvoiceMangroup, vo);
            List<ChargeInvoiceMangroupType> chargeInvoiceMangroupTypeList = chargeInvoiceMangroupTypeService.selectByMainId(chargeInvoiceMangroup.getId());
            vo.setChargeInvoiceMangroupTypeList(chargeInvoiceMangroupTypeList);
            List<ChargeInvoiceMangroupPosition> chargeInvoiceMangroupPositionList = chargeInvoiceMangroupPositionService.selectByMainId(chargeInvoiceMangroup.getId());
            vo.setChargeInvoiceMangroupPositionList(chargeInvoiceMangroupPositionList);
            List<ChargeInvoiceManpeople> chargeInvoiceManpeopleList = chargeInvoiceManpeopleService.selectByMainId(chargeInvoiceMangroup.getId());
            vo.setChargeInvoiceManpeopleList(chargeInvoiceManpeopleList);
            pageList.add(vo);
        }
        //导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "票据组列表");
        mv.addObject(NormalExcelConstants.CLASS, ChargeInvoiceMangroupPage.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("票据组列表数据", "导出人:Jeecg", "导出信息"));
        mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
        return mv;
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile file = entity.getValue();// 获取上传文件对象
            ImportParams params = new ImportParams();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try {
                List<ChargeInvoiceMangroupPage> list = ExcelImportUtil.importExcel(file.getInputStream(), ChargeInvoiceMangroupPage.class, params);
                for (ChargeInvoiceMangroupPage page : list) {
                    ChargeInvoiceMangroup po = new ChargeInvoiceMangroup();
                    BeanUtils.copyProperties(page, po);
                    chargeInvoiceMangroupService.saveMain(po, page.getChargeInvoiceMangroupTypeList(),page.getChargeInvoiceMangroupPositionList(),page.getChargeInvoiceManpeopleList());
                }
                return Result.ok("文件导入成功！数据行数:" + list.size());
            } catch (Exception e) {
                log.error(e.getMessage(),e);
                return Result.error("文件导入失败:"+e.getMessage());
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.ok("文件导入失败！");
    }

    /**
     * 校验票据组数据是否在系统中是否存在
     *
     * @return
     */
    @RequestMapping(value = "/checkMangroup", method = RequestMethod.GET)
    @ApiOperation("票据组重复校验接口")
    public Result<Object> doDuplicateCheck(CheckOutMangroup checkOutMangroup, HttpServletRequest request) {
        Long num = null;

        log.info("----duplicate check------："+ checkOutMangroup.toString());
        //票据组对应票据类型重复校验
        if (checkOutMangroup.getTableName().equals("charge_invoice_mangroup_type")){
            if (StringUtils.isNotBlank(checkOutMangroup.getDataId())) {
                // [2].编辑页面校验
                num = chargeInvoiceMangroupService.duplicateCheckCountSql(checkOutMangroup);
            } else {
                // [1].添加页面校验
                num = chargeInvoiceMangroupService.duplicateCheckCountSqlNoDataId(checkOutMangroup);
            }
        }
        //票据组对应岗位重复校验
        if (checkOutMangroup.getTableName().equals("charge_invoice_mangroup_position")){
            if (StringUtils.isNotBlank(checkOutMangroup.getDataId())) {
                // [2].编辑页面校验
                num = chargeInvoiceMangroupService.duplicateCheckPositionCountSql(checkOutMangroup);
            } else {
                // [1].添加页面校验
                String officeId = checkOutMangroup.getFieldVal();
                String offId[] = officeId.split(",");

                num = chargeInvoiceMangroupService.duplicateCheckPositionCountSqlNoDataId(checkOutMangroup,offId);
            }
        }
        //票据组对应人员重复校验
        if (checkOutMangroup.getTableName().equals("charge_invoice_manpeople")){
            if (StringUtils.isNotBlank(checkOutMangroup.getDataId())) {
                // [2].编辑页面校验
                num = chargeInvoiceMangroupService.duplicateCheckPeopleCountSql(checkOutMangroup);
            } else {
                // [1].添加页面校验
                String userIdArr = checkOutMangroup.getFieldVal();
                String userId[] = userIdArr.split(",");

                num = chargeInvoiceMangroupService.duplicateCheckPeopleCountSqlNoDataId(checkOutMangroup,userId);
            }
        }

        if (num == null || num == 0) {
            // 该值可用
            return Result.ok("该值可用！");
        } else {
            // 该值不可用
            log.info("该值不可用，系统中已存在！");
            return Result.error("该值不可用，系统中已存在！");
        }
    }
}
